package com.winter.Controller;

import com.winter.model.vo.AliPayVO;
import com.winter.model.vo.WxPayVo;
import com.winter.payService.AliPayService;
import com.winter.payService.WxPayService;
import com.winter.util.Result;
import com.winter.util.ResultCode;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("v1/pay")
public class PayController {

    @Autowired
    private AliPayService aliPayService;

    @Autowired
    private WxPayService wxPayService;

    /**
     * 支付宝支付（课件下载）
     */
    @PostMapping("ali")
    public Result aliPay(@RequestBody AliPayVO payVO) {
        try {
            return aliPayService.returnAlipayPayParam(payVO.getBody(), payVO.getSubject(), payVO.getOut_trade_no(), payVO.getTotal_amount(), payVO.getParam());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
    }

    /**
     * 支付宝支付（课件下载）
     */
    @RequestMapping("aliNotify")
    public String aliNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            return aliPayService.aliNotify(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ERROR";
    }

    /**
     * 微信支付
     */
    @PostMapping("wx")
    public Result wxPay(@RequestBody WxPayVo wxPayVo) {
        try {
            return wxPayService.wxPay(wxPayVo.getBody(), wxPayVo.getOut_trade_no(), wxPayVo.getTotal_amount(), wxPayVo.getParam());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.failure(ResultCode.SYSTEM_INNER_ERROR);
    }

    /**
     * 微信支付回调
     */
    @RequestMapping("wxNotify")
    public void wxNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            wxPayService.wxNotify(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
